package com.bwie.init;

import com.bwie.domain.Product;
import com.bwie.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * TODO
 *
 * @Description
 * @Author 36167
 * @Date 2025/3/8 11:03
 **/

@Component
public class InitDate  {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Autowired
    private ProductMapper productMapper;

   @PostConstruct
    public void a1() {
        IndexOperations indexOps = restTemplate.indexOps(Product.class);
        boolean exists = indexOps.exists();
        if (!exists){
            indexOps.create();
            Document mapping = indexOps.createMapping();
            indexOps.putMapping(mapping);
        }
//        List<Product> products = productMapper.selectBatchIds(null);
       List<Product> products = productMapper.selectList(null);
       for (Product product : products) {
            restTemplate.save(product);
        }
    }
}
